package br.inatel.ec203.projeto.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class TransistorSpecificPanel extends JPanel {

	private static final long serialVersionUID = -2206116829471384379L;
	
	private TransistorSpecificPanelListeners transistorSpecificPanelListeners;

	private JRadioButton fetRadioButton;
	private JRadioButton bjtRadioButton;
	
	private JLabel vdsLabel;
	private JTextField vdsTextField;
	private JLabel vgsLabel;
	private JTextField vgsTextField;
	private JLabel idLabel;
	private JTextField idTextField;
	private JLabel rdsOnLabel;
	private JTextField rdsOnTextField;
	
	private JLabel channelLabel;
	private JComboBox<String> channelComboBox;
	
	private JLabel vceLabel;
	private JTextField vceTextField;
	private JLabel icLabel;
	private JTextField icTextField;
	private JLabel junctionLabel;
	
	private GridBagConstraints fetRadioButtonConstraints;
	private GridBagConstraints bjtRadioButtonConstraints;
	
	private GridBagConstraints vdsLabelConstraints;
	private GridBagConstraints vdsTextFieldConstraints;
	private GridBagConstraints vgsLabelConstraints;
	private GridBagConstraints vgsTextFieldConstraints;
	private GridBagConstraints idLabelConstraints;
	private GridBagConstraints idTextFieldConstraints;
	private GridBagConstraints rdsOnLabelConstraints;
	private GridBagConstraints rdsOnTextFieldConstraints;
	
	private GridBagConstraints channelLabelConstraints;
	private GridBagConstraints channelComboBoxConstraints;
	
	private GridBagConstraints vceLabelConstraints;
	private GridBagConstraints vceTextFieldConstraints;
	private GridBagConstraints icLabelConstraints;
	private GridBagConstraints icTextFieldConstraints;
	private GridBagConstraints junctionLabelConstraints;
	
	public TransistorSpecificPanel() {
		initialize();
	}
	
	private void initialize() {
		setLayout(new GridBagLayout());
		add(getFetRadioButton(), getFetRadioButtonConstraints());
		add(getBjtRadioButton(), getBjtRadioButtonConstraints());		
	}

	public JRadioButton getFetRadioButton() {
		if (fetRadioButton == null) {
			fetRadioButton = new JRadioButton();
			fetRadioButton.setText("FET");
			fetRadioButton.setSelected(false);
			fetRadioButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					transistorSpecificPanelListeners.fetPerformed();
					getBjtRadioButton().setSelected(false);
				}
			});
		}
		return fetRadioButton;
	}
	public JRadioButton getBjtRadioButton() {
		if (bjtRadioButton == null) {
			bjtRadioButton = new JRadioButton();
			bjtRadioButton.setText("BJT");
			bjtRadioButton.setSelected(false);
			bjtRadioButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					transistorSpecificPanelListeners.bjtPerformed();
					getFetRadioButton().setSelected(false);
				}
			});
		}
		return bjtRadioButton;
	}
	public JLabel getVdsLabel() {
		if (vdsLabel == null) {
			vdsLabel = new JLabel();
			vdsLabel.setText("Vds Max");
		}
		return vdsLabel;
	}
	public JTextField getVdsTextField() {
		if (vdsTextField == null) {
			vdsTextField = new JTextField();
		}
		return vdsTextField;
	}
	public JLabel getVgsLabel() {
		if (vgsLabel == null) {
			vgsLabel = new JLabel();
			vgsLabel.setText("Vgs Max");
		}
		return vgsLabel;
	}
	public JTextField getVgsTextField() {
		if (vgsTextField == null) {
			vgsTextField = new JTextField();
		}
		return vgsTextField;
	}
	public JLabel getIdLabel() {
		if (idLabel == null) {
			idLabel = new JLabel();
			idLabel.setText("Id Max");
		}
		return idLabel;
	}
	public JTextField getIdTextField() {
		if (idTextField == null) {
			idTextField = new JTextField();			
		}
		return idTextField;
	}
	public JLabel getRdsOnLabel() {
		if (rdsOnLabel == null) {
			rdsOnLabel = new JLabel();
			rdsOnLabel.setText("Rds On");
		}
		return rdsOnLabel;
	}
	public JTextField getRdsOnTextField() {
		if (rdsOnTextField == null) {
			rdsOnTextField = new JTextField();
		}
		return rdsOnTextField;
	}
	public JLabel getChannelLabel() {
		if (channelLabel == null) {
			channelLabel = new JLabel();
			channelLabel.setText("Canal");
		}
		return channelLabel;
	}
	public JComboBox<String> getChannelComboBox() {
		if (channelComboBox == null) {
			channelComboBox = new JComboBox<String>();
			channelComboBox.addItem(" ");
			channelComboBox.addItem("N");
			channelComboBox.addItem("P");
		}
		return channelComboBox;
	}
	public JLabel getVceLabel() {
		if (vceLabel == null) {
			vceLabel = new JLabel();
			vceLabel.setText("Vce Maximo");
		}
		return vceLabel;
	}
	public JTextField getVceTextField() {
		if (vceTextField == null) {
			vceTextField = new JTextField();
		}
		return vceTextField;
	}
	public JLabel getIcLabel() {
		if (icLabel == null) {
			icLabel = new JLabel();
			icLabel.setText("Ic Maximo");			
		}
		return icLabel;
	}
	public JTextField getIcTextField() {
		if (icTextField == null) {
			icTextField = new JTextField();
		}
		return icTextField;
	}		
	public JLabel getJunctionLabel() {
		if (junctionLabel == null) {
			junctionLabel = new JLabel();
			junctionLabel.setText("Juncao");
		}
		return junctionLabel;
	}
	public GridBagConstraints getFetRadioButtonConstraints() {
		if (fetRadioButtonConstraints == null) {
			fetRadioButtonConstraints = new GridBagConstraints();
			fetRadioButtonConstraints.gridx = 2;
			fetRadioButtonConstraints.gridy = 0;
			fetRadioButtonConstraints.gridwidth = 2;
			fetRadioButtonConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return fetRadioButtonConstraints;
	}
	public GridBagConstraints getBjtRadioButtonConstraints() {
		if (bjtRadioButtonConstraints == null) {
			bjtRadioButtonConstraints = new GridBagConstraints();
			bjtRadioButtonConstraints.gridx = 4;
			bjtRadioButtonConstraints.gridy = 0;
			bjtRadioButtonConstraints.gridwidth = 2;
			bjtRadioButtonConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return bjtRadioButtonConstraints;
	}
	public GridBagConstraints getVdsLabelConstraints() {
		if (vdsLabelConstraints == null) {
			vdsLabelConstraints = new GridBagConstraints();
			vdsLabelConstraints.gridx = 0;
			vdsLabelConstraints.gridy = 1;
			vdsLabelConstraints.anchor = GridBagConstraints.LINE_START;
			vdsLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return vdsLabelConstraints;
	}
	public GridBagConstraints getVdsTextFieldConstraints() {
		if (vdsTextFieldConstraints == null) {
			vdsTextFieldConstraints = new GridBagConstraints();
			vdsTextFieldConstraints.gridx = 1;
			vdsTextFieldConstraints.gridy = 1;
			vdsTextFieldConstraints.ipadx = 40;
			vdsTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
			vdsTextFieldConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return vdsTextFieldConstraints;
	}
	public GridBagConstraints getVgsLabelConstraints() {
		if (vgsLabelConstraints == null) {
			vgsLabelConstraints = new GridBagConstraints();
			vgsLabelConstraints.gridx = 2;
			vgsLabelConstraints.gridy = 1;
			vgsLabelConstraints.anchor = GridBagConstraints.LINE_START;
			vgsLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return vgsLabelConstraints;
	}
	public GridBagConstraints getVgsTextFieldConstraints() {
		if (vgsTextFieldConstraints == null) {
			vgsTextFieldConstraints = new GridBagConstraints();
			vgsTextFieldConstraints.gridx = 3;
			vgsTextFieldConstraints.gridy = 1;
			vgsTextFieldConstraints.ipadx = 40;
			vgsTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
			vgsTextFieldConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return vgsTextFieldConstraints;
	}
	public GridBagConstraints getIdLabelConstraints() {
		if (idLabelConstraints == null) {
			idLabelConstraints = new GridBagConstraints();
			idLabelConstraints.gridx = 4;
			idLabelConstraints.gridy = 1;
			idLabelConstraints.anchor = GridBagConstraints.LINE_START;
			idLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return idLabelConstraints;
	}
	public GridBagConstraints getIdTextFieldConstraints() {
		if (idTextFieldConstraints == null) {
			idTextFieldConstraints = new GridBagConstraints();
			idTextFieldConstraints.gridx = 5;
			idTextFieldConstraints.gridy = 1;
			idTextFieldConstraints.ipadx = 40;
			idTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
			idTextFieldConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return idTextFieldConstraints;
	}
	public GridBagConstraints getRdsOnLabelConstraints() {
		if (rdsOnLabelConstraints == null) {
			rdsOnLabelConstraints = new GridBagConstraints();
			rdsOnLabelConstraints.gridx = 6;
			rdsOnLabelConstraints.gridy = 1;
			rdsOnLabelConstraints.anchor = GridBagConstraints.LINE_START;
			rdsOnLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return rdsOnLabelConstraints;
	}
	public GridBagConstraints getRdsOnTextFieldConstraints() {
		if (rdsOnTextFieldConstraints == null) {
			rdsOnTextFieldConstraints = new GridBagConstraints();
			rdsOnTextFieldConstraints.gridx = 7;
			rdsOnTextFieldConstraints.gridy = 1;
			rdsOnTextFieldConstraints.ipadx = 40;
			rdsOnTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
			rdsOnTextFieldConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return rdsOnTextFieldConstraints;
	}
	public GridBagConstraints getChannelLabelConstraints() {
		if (channelLabelConstraints == null) {
			channelLabelConstraints = new GridBagConstraints();
			channelLabelConstraints.gridx = 3;
			channelLabelConstraints.gridy = 2;
			channelLabelConstraints.anchor = GridBagConstraints.CENTER;
			channelLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return channelLabelConstraints;
	}
	public GridBagConstraints getChannelComboBoxConstraints() {
		if (channelComboBoxConstraints == null) {
			channelComboBoxConstraints = new GridBagConstraints();
			channelComboBoxConstraints.gridx = 4;
			channelComboBoxConstraints.gridy = 2;
			channelComboBoxConstraints.fill = GridBagConstraints.HORIZONTAL;
			channelComboBoxConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return channelComboBoxConstraints;
	}
	public GridBagConstraints getVceLabelConstraints() {
		if (vceLabelConstraints == null) {
			vceLabelConstraints = new GridBagConstraints();
			vceLabelConstraints.gridx = 2;
			vceLabelConstraints.gridy = 1;
			vceLabelConstraints.anchor = GridBagConstraints.LINE_START;
			vceLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return vceLabelConstraints;
	}
	public GridBagConstraints getVceTextFieldConstraints() {
		if (vceTextFieldConstraints == null) {
			vceTextFieldConstraints = new GridBagConstraints();
			vceTextFieldConstraints.gridx = 3;
			vceTextFieldConstraints.gridy = 1;
			vceTextFieldConstraints.ipadx = 40;
			vceTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
			vceTextFieldConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return vceTextFieldConstraints;
	}
	public GridBagConstraints getIcLabelConstraints() {
		if (icLabelConstraints == null) {
			icLabelConstraints = new GridBagConstraints();
			icLabelConstraints.gridx = 4;
			icLabelConstraints.gridy = 1;
			icLabelConstraints.anchor = GridBagConstraints.LINE_START;
			icLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return icLabelConstraints;
	}
	public GridBagConstraints getIcTextFieldConstraints() {
		if (icTextFieldConstraints == null) {
			icTextFieldConstraints = new GridBagConstraints();
			icTextFieldConstraints.gridx = 5;
			icTextFieldConstraints.gridy = 1;
			icTextFieldConstraints.ipadx = 40;
			icTextFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
			icTextFieldConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return icTextFieldConstraints;
	}
	public GridBagConstraints getJunctionLabelConstraints() {
		if (junctionLabelConstraints == null) {
			junctionLabelConstraints = new GridBagConstraints();
			junctionLabelConstraints.gridx = 3;
			junctionLabelConstraints.gridy = 2;
			junctionLabelConstraints.anchor = GridBagConstraints.CENTER;
			junctionLabelConstraints.insets = new Insets(5, 5, 5, 5);
		}
		return junctionLabelConstraints;
	}
	public void addTransistorSpecificPanelListeners(
			TransistorSpecificPanelListeners transistorSpecificPanelListeners) {
		this.transistorSpecificPanelListeners = transistorSpecificPanelListeners;
	}
	
	public ArrayList<String> getData(){
		ArrayList<String> data = new ArrayList<String>();
		
		data.add(Util.addInArrayList(getVdsTextField().getText()));
		data.add(Util.addInArrayList(getVgsTextField().getText()));
		data.add(Util.addInArrayList(getIdTextField().getText()));
		data.add(Util.addInArrayList(getRdsOnTextField().getText()));
		int selecteIndex = getChannelComboBox().getSelectedIndex();
		String channel = null;
		if (selecteIndex == 1) {
			channel = "N";
		}else if (selecteIndex == 2) {
			channel = "P";
		}
		data.add(Util.addInArrayList(channel));
		data.add(Util.addInArrayList(getVceTextField().getText()));
		data.add(Util.addInArrayList(getIcTextField().getText()));
		
		return data;
	}
}













